const BannerModel = require('../model/banner')

exports.uploadPhoto = async (req, res, next) => {
    if (req.files.banner_img.size === 0) return next({ status: 'fail', msg: '没有提交任何图片' })
    try {
        let result = await BannerModel.uploadPhoto(req.files)
        // console.log(result);
        res.send({ status: 'success', msg: '图片上传成功', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '图片上传失败' })
    }
}

exports.add = async (req, res, next) => {
    try {
        let result = await BannerModel.add(req.fields)
        // console.log(result);
        res.send({ status: 'success', msg: 'banner添加成功', data: { id: result[0].insertId } })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: 'banner添加失败' })
    }
}

exports.get = async (req, res, next) => {
    try {
        // console.log(req.params);
        let result = await BannerModel.get()
        res.send({ status: 'success', msg: 'banner获取成功', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: 'banner获取失败' })
    }
}

